<template>
  <div class="app-container">
    <el-row class="pagecenter">
      <el-form :inline="true" style="float:right">
        <el-form-item label-width="80px">
          <el-button @click="createRole" v-show="btnVisible.isAdd">
            <span>
              <icon-svg icon-class="xinjian"></icon-svg>&nbsp;&nbsp;新建角色</span>
          </el-button>
        </el-form-item>
      </el-form>
    </el-row>
    <el-row class="pageborder">
      <el-table :data="list" v-loading.body="listLoading" element-loading-text="拼命加载中" highlight-current-row tooltip-effect="dark" style="width: 100%" :fit="true">
        <el-table-column align="center" label='序号' width="80">
          <template scope="scope">
            {{ page.pageSize * (page.currentPage - 1) + scope.$index + 1}}
          </template>
        </el-table-column>
        <!-- <el-table-column align="center" label='角色编号' width="120">
              <template scope="scope">
                {{scope.row.roleid}}
              </template>
            </el-table-column> -->
        <el-table-column label="角色名称" min-width="150" header-align="center" align="left">
          <template scope="scope">
            {{scope.row.rolename}}
          </template>
        </el-table-column>
        <el-table-column label="拥有权限" min-width="760" :show-overflow-tooltip="true" header-align="center" align="left">
          <template scope="scope">
            <span>{{scope.row.ownPermissionName}}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" min-width="180" align="center" header-align="center">
          <template scope="scope">
            <el-button icon="edit" type="small" @click="editRole(scope.$index,scope.row)" v-show="(scope.row.roleid !== 1 && scope.row.roleid !== 2 && scope.row.roleid !== 3 && scope.row.roleid !== 4) && btnVisible.isEdit">编辑</el-button>
            <el-button icon="delete" type="small" @click="deleteRole(scope.$index,scope.row)" v-show="(scope.row.roleid !== 1 && scope.row.roleid !== 2 && scope.row.roleid !== 3 && scope.row.roleid !== 4) && btnVisible.isDelete">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-row>
    <el-row class="pagination" v-if="page.total > 0 ? true : false">
      <el-pagination @current-change="handleCurrentChange" :page-size="page.pageSize" :current-page.sync="page.currentPage" layout="total, prev, pager, next, jumper" :total="page.total">
      </el-pagination>
    </el-row>
    <!-- <el-dialog title="提示" :visible.sync="dialogVisible" size="tiny" :before-close="handleClose">
          <span>{{tipInfo}}</span>
          <span slot="footer" class="dialog-footer">
            <el-button @click="dialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
          </span>
        </el-dialog> -->
  </div>
</template>

<style scoped>
.pagination {
  text-align: center;
}

.el-col {
  border-radius: 4px;
}

.grid-content {
  border-radius: 4px;
  min-height: 36px;
}

.bg-purple-dark {
  background: #99a9bf;
}

.colSpace {
  margin-left: 5px;
}

td {
  overflow: hidden;
  text-overflow: ellipsis;
}

.viewBtn {
  float: right;
}

.selectStyle {
  width: 145px;
}

.el-form-item {
  margin-bottom: 15px;
}
</style>

<script>
import { getRolesList, deleteRole } from 'api/system';
import { parseTime } from 'utils/index';
import { btnIsVisible, trimStr } from 'utils/customer';

export default {
  data() {
    return {
      list: null,
      listQuery: {},
      page: {
        pageSize: 10,
        total: 0,
        totalPage: 0,
        currentPage: 0
      },
      listLoading: true,
      dialogVisible: false,
      tipInfo: '',
      btnVisible: {
        isAdd: false,
        isEdit: false,
        isDelete: false,
      }
    }
  },
  created() {
    this.fetchData();
    this.getBtnVisbleStatus();
  },
  methods: {
    getBtnVisbleStatus() {
      this.btnVisible.isAdd = btnIsVisible('AUTHORITY_CONFIG_INSERT') || false;
      this.btnVisible.isEdit = btnIsVisible('AUTHORITY_CONFIG_UPDATE') || false;
      this.btnVisible.isDelete = btnIsVisible('AUTHORITY_CONFIG_DELETE') || false;
    },
    fetchData() {
      this.listLoading = true;
      this.page.pageSize = this.page.pageSize || 10;
      this.page.currentPage = this.page.currentPage || 1;
      this.listQuery.currentPage = this.page.currentPage;
      this.listQuery.pageSize = this.page.pageSize;
      getRolesList(this.listQuery).then(response => {
        let res = response.data;
        if (res.flag) {
          this.list = res.data;
          this.page = res.page;
        }
        this.listLoading = false;
      })
    },
    createRole() {
      let role = {
        roleid: null,
        rolename: null,
        platform: null,
        rolePermission: []
      }
      this.$router.push({ path: 'roleAdd', query: { role: role } });
    },
    editRole(index, row) {
      this.$router.push({ path: 'roleedit', query: { role: row } });
    },
    deleteRole(index, row) {
      this.$confirm('此操作将会永久删除该记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteRole(row).then(response => {
          let res = response.data;
          if (res.flag === 1) {
            this.fetchData()
            this.$message({
              showClose: true,
              message: '删除角色成功',
              type: 'success'
            });
          } else {
            this.tipInfo = res.message;
            this.$message({
              showClose: true,
              message: this.tipInfo,
              type: 'error'
            });
          }
        })
      }).catch(() => {
      });
      // deleteRole(row).then(response => {
      //   let res = response.data;
      //   if (res.flag === 1) {
      //     this.fetchData()
      //   } else {
      //     this.tipInfo = res.message;
      //     this.dialogVisible = true
      //   }
      // });
    },
    formatJson(filterVal, jsonData) {
      return jsonData.map(v => filterVal.map(j => v[j]))
    },
    handleDivisionChange(val) {
      // console.log(val);
    },
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`);
    },
    handleCurrentChange(val) {
      this.page.currentPage = val;
      this.fetchData();
    },
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          done();
        })
        .catch(_ => { });
    }
  }
};
</script>